#include "xchain/xchain.h"
#include <string>

struct SimpleStorage : public xchain::Contract {
public:
    void initialize() {
        xchain::Context* ctx = this->context();
        ctx->put_object("default", "Hello XuperChain");
        ctx->ok("SimpleStorage initialized successfully");
    }
    
    void set() {
        xchain::Context* ctx = this->context();
        std::string key = ctx->arg("key");
        std::string value = ctx->arg("value");
        
        if (key.empty()) {
            ctx->error("Key cannot be empty");
            return;
        }
        
        if (ctx->put_object(key, value)) {
            ctx->ok("Data stored successfully");
            return;
        }
        ctx->error("Failed to store data");
    }
    
    void get() {
        xchain::Context* ctx = this->context();
        std::string key = ctx->arg("key");
        
        if (key.empty()) {
            ctx->error("Key cannot be empty");
            return;
        }
        
        std::string value;
        if (ctx->get_object(key, &value)) {
            ctx->ok(value);
            return;
        }
        ctx->ok(""); // 返回空字符串，类似Solidity中未找到键的行为
    }
};

DEFINE_METHOD(SimpleStorage, initialize) {
    self.initialize();
}

DEFINE_METHOD(SimpleStorage, set) {
    self.set();
}

DEFINE_METHOD(SimpleStorage, get) {
    self.get();
}